package com.test.voice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity implements OnClickListener {

	private static final int VOICE_RECOGNITION_CODE = 3912;

	private Button buttonDialog;
	private Button buttonRecognitionListener;
	private TextView textViewStatus;

	private SpeechRecognizer speechRecognizer;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		buttonDialog = (Button) findViewById(R.id.buttonDialog);
		buttonRecognitionListener = (Button) findViewById(R.id.buttonRecognitionListener);
		textViewStatus = (TextView) findViewById(R.id.textViewStatus);

		buttonDialog.setOnClickListener(this);
		buttonRecognitionListener.setOnClickListener(this);

		textViewStatus.setText("Waiting on you.");
	}

	@Override
	public void onClick(View v) {
		final Intent intent;
		switch (v.getId()) {
		case R.id.buttonDialog:
			intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
					RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
					"Say something already!");
			startActivityForResult(intent, VOICE_RECOGNITION_CODE);
			break;
		case R.id.buttonRecognitionListener:
			buttonRecognitionListener.setEnabled(false);
			if (SpeechRecognizer
					.isRecognitionAvailable(getApplicationContext())) {

				// Create the recognition listener
				speechRecognizer = SpeechRecognizer
						.createSpeechRecognizer(getApplicationContext());
				speechRecognizer.setRecognitionListener(new VoiceListener());

				// Create the intent to launch speech to text
				intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
						RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
				intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
						"com.test.voice");
				intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);

				// FIRE
				speechRecognizer.startListening(intent);
			} else {

				// =(
				textViewStatus.setText("Speech Recognition unavailable");
				buttonRecognitionListener.setEnabled(true);
			}
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (resultCode) {
		case RESULT_OK:
			ArrayList<String> matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			textViewStatus.setText("Result: " + matches.get(0));
			break;
		default:
			textViewStatus.setText("Some error has occured.");
			break;
		}

	}

	private class VoiceListener implements RecognitionListener {

		@Override
		public void onBeginningOfSpeech() {
			textViewStatus.setText("Listening to you talk");
		}

		@Override
		public void onBufferReceived(byte[] buffer) {
		}

		@Override
		public void onEndOfSpeech() {
			textViewStatus.setText("Processing...");
		}

		@Override
		public void onError(int error) {
			String msg = "";
			switch (error) {
			case SpeechRecognizer.ERROR_AUDIO:
				msg = "ERROR_AUDIO";
				break;
			case SpeechRecognizer.ERROR_CLIENT:
				msg = "ERROR_CLIENT";
				break;
			case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
				msg = "ERROR_INSUFFICIENT_PERMISSIONS";
				break;
			case SpeechRecognizer.ERROR_NETWORK:
				msg = "ERROR_NETWORK";
				break;
			case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
				msg = "ERROR_NETWORK_TIMEOUT";
				break;
			case SpeechRecognizer.ERROR_NO_MATCH:
				msg = "ERROR_NO_MATCH";
				break;
			case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
				msg = "ERROR_RECOGNIZER_BUSY";
				break;
			case SpeechRecognizer.ERROR_SERVER:
				msg = "ERROR_SERVER";
				break;
			case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
				msg = "ERROR_SPEECH_TIMEOUT";
				break;
			}
			textViewStatus.setText(msg);
			buttonRecognitionListener.setEnabled(true);
		}

		@Override
		public void onEvent(int eventType, Bundle params) {
		}

		@Override
		public void onPartialResults(Bundle partialResults) {
		}

		@Override
		public void onReadyForSpeech(Bundle params) {
			textViewStatus.setText("Start talking!");
		}

		@Override
		public void onResults(Bundle results) {
			buttonRecognitionListener.setEnabled(true);
			List<String> resultsList = results
					.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

			if (resultsList == null) {
				textViewStatus.setText("Speech to text returns null =(");
				return;
			}

			String result = Arrays.toString(resultsList.toArray(new String[0]));
			textViewStatus.setText("Result: " + result);
		}

		@Override
		public void onRmsChanged(float rmsdB) {
		}

	}

}
